package pl.edu.agh.neuraleconomy.ui.component;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;

import lombok.Getter;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import pl.edu.agh.neuraleconomy.core.simulation.SimulationService;
import pl.edu.agh.neuraleconomy.model.exchange.Company;

public class TransactionDialog extends OkCancelDialog {

	private Collection<Company> companyList;
	@Getter
	private Date currentDate;
	private SimulationService service = new SimulationService();
	private Combo companyCombo;
	private Text amountText;
	private Label price;
	private Label totalPrice;

	@Getter
	private Company company;
	@Getter
	private int ammount;

	public TransactionDialog(Shell parentShell, Collection<Company> companyList, Date currentDate) {
		super(parentShell);
		this.currentDate = currentDate;
		this.companyList = companyList;
	}

	public void selectCompany(Company company) {
		if (company != null && companyList.contains(company)) {
			companyCombo.setText(company.getName());
			refreshPrice();
		}
	}

	@Override
	protected void init(Composite parent) {

		parent.setLayout(new GridLayout(2, false));
		(new Label(parent, SWT.NONE)).setText("Company");
		companyCombo = new Combo(parent, SWT.READ_ONLY);
		populateCombo();

		(new Label(parent, SWT.NONE)).setText("Ammount");
		amountText = new Text(parent, SWT.NONE);
		amountText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		amountText.setText("1");
		amountText.addModifyListener(new ModifyListener() {

			public void modifyText(ModifyEvent arg0) {
				refreshTotalPrice(BigDecimal.valueOf(Double.parseDouble(price.getText())));

			}
		});

		(new Label(parent, SWT.NONE)).setText("Price");
		price = new Label(parent, SWT.NONE);
		price.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		price.setText("");

		(new Label(parent, SWT.NONE)).setText("Total price");
		totalPrice = new Label(parent, SWT.NONE);
		totalPrice.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		totalPrice.setText("");

		refreshPrice();
	}

	private void populateCombo() {
		for (Company c : companyList) {
			String companyName = c.getName();
			companyCombo.add(companyName);
			companyCombo.setData(companyName, c);
		}

		if (companyCombo.getItemCount() > 0) {
			companyCombo.setText(companyCombo.getItem(0));
		}
		
		companyCombo.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				refreshPrice();
			}
		});
	}

	private Company getSelectedCompany() {
		int indx = companyCombo.getSelectionIndex();
		if(indx < 0){
			return null;
		}
		String selectedCompanyName = companyCombo.getItem(indx);
		return (Company) companyCombo.getData(selectedCompanyName);
	}

	@Override
	protected void saveInput() {
		ammount = Integer.parseInt(amountText.getText());
		company = getSelectedCompany();
	}

	@Override
	protected boolean validateInput() {
		try {
			return (Integer.parseInt(amountText.getText()) > 0);
		} catch (Exception e) {
			return false;
		}
	}

	private void refreshPrice() {
		Company selected = getSelectedCompany();
		BigDecimal price = selected == null ? BigDecimal.ZERO.setScale(2) : service.getSharePrice(selected, currentDate);
		this.price.setText(String.valueOf(price));
		refreshTotalPrice(price);
	}

	private void refreshTotalPrice(BigDecimal price) {
		try {
			int ammount = Integer.parseInt(this.amountText.getText());
			BigDecimal total = price.multiply(BigDecimal.valueOf(ammount));
			totalPrice.setText(String.valueOf(total));
		} catch (Exception e) {
			this.totalPrice.setText("0.0");
		}
	}

}
